//package airAgent.group5.garbage;
//
//
///*
// *  Alternative version of airline
// *  
// *  after every request waits for answer from bank
// */
//
//import airAgent.group5.ontology.ontology.*;
//
//import jade.content.lang.Codec;
//import jade.content.lang.Codec.*;
//import jade.content.lang.sl.*;
//import jade.content.*;
//import jade.content.onto.*;
//import jade.content.onto.basic.*;
////---
//import jade.core.AID;
//import jade.core.Agent;
//import jade.core.behaviours.SimpleBehaviour;
//import jade.domain.DFService;
//import jade.domain.FIPAException;
//import jade.domain.FIPAAgentManagement.DFAgentDescription;
//import jade.domain.FIPAAgentManagement.ServiceDescription;
//import jade.lang.acl.ACLMessage;
//import jade.lang.acl.MessageTemplate;
//
//public class Airline5_2 extends Agent{
//	private static String NAME = "G5";
//	airAgent.group5.ontology.ontology.Bank ontoBank = new airAgent.group5.ontology.ontology.Bank();
//	private static String BANK = "BANK";
//	private boolean hasBankAccount = false;
//	private AID bankRef;
//	
//    private Codec codec = new SLCodec();
//	private Ontology ontology = AirAgentOntology.getInstance();
//	
//	protected void setup() {
//		System.out.println(getLocalName()+": HAS ENTERED");
//        getContentManager().registerLanguage(codec);
//		getContentManager().registerOntology(ontology);
//		try{	
//			// Creates its own description
//			DFAgentDescription dfd = new DFAgentDescription();
//			ServiceDescription sd = new ServiceDescription();
//			sd.setName(this.getName());
//			sd.setType(NAME);
//			dfd.addServices(sd);
//			// Registers its description in the DF
//			DFService.register(this, dfd);
//			System.out.println(getLocalName()+": registered in the DF");
//		} 
//		catch (FIPAException e)
//		{
//			e.printStackTrace();
//		}
//		
////		BEHAVIOURS ****************************************************************
//
//		// Adds a behavior to search a bank agent and request a bank account	
//		
//		addBehaviour(new SimpleBehaviour(this) {
//			private boolean end = false;
//			private int tries = 0;
//
//			public void action() {
//				tries++;
//				System.out.println(tries + " try to call for a bank! (Create Account)");
//				// Creates the description for the type of agent to be searched
//				DFAgentDescription dfd = new DFAgentDescription();
//				ServiceDescription sd = new ServiceDescription();
//				sd.setType(BANK);		// SHOULD USE THE FUCKIN ONTOLOGY CABRON (ontoBank)
//				dfd.addServices(sd);
//				
//				try {
//					// It finds agents of the required type
//					DFAgentDescription[] res = new DFAgentDescription[1];
//					res = DFService.search(myAgent, dfd);
//					if (res.length > 0) {
//						bankRef = (AID)res[0].getName();
//						ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
//						msg.setContent(NAME);
//						msg.addReceiver(bankRef);
//						msg.setLanguage(codec.getName());
//						msg.setOntology(ontology.getName());
//						msg.setProtocol(AirAgentOntology.CREATEACCOUNT);
//						CreateAccount ca = new CreateAccount();
//						// As it is an action and the encoding language the SL, it must be wrapped
//						// into an Action
//						Action agAction = new Action(bankRef,ca);
//						try {
//							// The ContentManager transforms the java objects into strings
//							getContentManager().fillContent(msg, agAction);
//							send(msg);
//							end = true;
//							System.out.println(getLocalName()+": asks for an account");
//							
//							receiveAnswer(bankRef); // !!!
//							
//						} catch (CodecException ce) {
//							ce.printStackTrace();
//						} catch (OntologyException oe) {
//							oe.printStackTrace();
//						}						
//					} else {
//						System.out.println("No bank found, wait 5 sekonds to procede");
//						doWait(5000);
//					}	
//				} catch (Exception e) {
//
//				}
//
//				if(tries == 3){
//					System.out.println("Couldn't find a bank in tree tries. Finishing!");
//					end = true;
//				}
//			}
//
//			public boolean done() {
//				return end;
//			}
//			
//		});
//		
//		//Behavior if the bank answers
//		// implemented as function receiveAnswer
//		
//		// Adds a behavior to request deposit money	
//		
//		addBehaviour(new SimpleBehaviour(this) {
//			private boolean end = false;
//			private int tries = 0;
//
//			public void action() {
//				tries++;
//				System.out.println(tries + " try to call for a bank! (Deposit)");
//				// Creates the description for the type of agent to be searched
//				DFAgentDescription dfd = new DFAgentDescription();
//				ServiceDescription sd = new ServiceDescription();
//				sd.setType(BANK);		// SHOULD USE THE FUCKIN ONTOLOGY CABRON (ontoBank)
//				dfd.addServices(sd);
//				
//				try {
//					// It finds agents of the required type
//					DFAgentDescription[] res = new DFAgentDescription[1];
//					res = DFService.search(myAgent, dfd);
//					if (res.length > 0) {
//						bankRef = (AID)res[0].getName();
//						ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
//						msg.setContent(NAME);
//						msg.addReceiver(bankRef);
//						msg.setLanguage(codec.getName());
//						msg.setOntology(ontology.getName());
//						msg.setProtocol(AirAgentOntology.DEPOSIT);
//						Deposit dep = new Deposit();
//						Money money = new Money();
//						money.setValue(66.6f);
//						dep.setAmount(money);
//						// As it is an action and the encoding language the SL, it must be wrapped
//						// into an Action
//						Action agAction = new Action(bankRef,dep);
//						try {
//							// The ContentManager transforms the java objects into strings
//							getContentManager().fillContent(msg, agAction);
//							send(msg);
//							end = true;
//							System.out.println(getLocalName()+": asks to deposit money");
//							
//							receiveAnswer(bankRef); // !!!
//							
//						} catch (CodecException ce) {
//							ce.printStackTrace();
//						} catch (OntologyException oe) {
//							oe.printStackTrace();
//						}						
//					} else {
//						System.out.println("No bank found, wait 5 sekonds to procede");
//						doWait(5000);
//					}	
//				} catch (Exception e) {
//
//				}
//
//				if(tries == 3){
//					System.out.println("Couldn't find a bank in tree tries. Finishing!");
//					end = true;
//				}
//			}
//
//			public boolean done() {
//				return end;
//			}
//			
//		});	
//		
//		// Adds a behavior to request withdraw money	
//		
//		addBehaviour(new SimpleBehaviour(this) {
//			private boolean end = false;
//			private int tries = 0;
//
//			public void action() {
//				tries++;
//				System.out.println(tries + " try to call for a bank! (Withdraw)");
//				// Creates the description for the type of agent to be searched
//				DFAgentDescription dfd = new DFAgentDescription();
//				ServiceDescription sd = new ServiceDescription();
//				sd.setType(BANK);		// SHOULD USE THE FUCKIN ONTOLOGY CABRON (ontoBank)
//				dfd.addServices(sd);
//				
//				try {
//					// It finds agents of the required type
//					DFAgentDescription[] res = new DFAgentDescription[1];
//					res = DFService.search(myAgent, dfd);
//					if (res.length > 0) {
//						bankRef = (AID)res[0].getName();
//						ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
//						msg.setContent(NAME);
//						msg.addReceiver(bankRef);
//						msg.setLanguage(codec.getName());
//						msg.setOntology(ontology.getName());
//						msg.setProtocol(AirAgentOntology.WITHDRAWN);
//						Withdrawn wit = new Withdrawn();
//						Money money = new Money();
//						money.setValue(11.1f);
//						wit.setAmount(money);
//						// As it is an action and the encoding language the SL, it must be wrapped
//						// into an Action
//						Action agAction = new Action(bankRef,wit);
//						try {
//							// The ContentManager transforms the java objects into strings
//							getContentManager().fillContent(msg, agAction);
//							send(msg);
//							end = true;
//							System.out.println(getLocalName()+": asks to withdraw money");
//							
//							receiveAnswer(bankRef); // !!!
//							
//						} catch (CodecException ce) {
//							ce.printStackTrace();
//						} catch (OntologyException oe) {
//							oe.printStackTrace();
//						}						
//					} else {
//						System.out.println("No bank found, wait 5 sekonds to procede");
//						doWait(5000);
//					}	
//				} catch (Exception e) {
//
//				}
//
//				if(tries == 3){
//					System.out.println("Couldn't find a bank in tree tries. Finishing!");
//					end = true;
//				}
//			}
//
//			public boolean done() {
//				return end;
//			}
//			
//		});			
//
//	}
//	
//	private void receiveAnswer(AID bankRef) {
//		//basicReceiveAnswer(bankRef);
//		//ontologicReceiveAnswer(bankRef);		// for group 3 and maybe others
//		protocolReceiveAnswer(bankRef);
//	}
//	
//	private void basicReceiveAnswer(AID bankRef) {
//		// Waits for the arrival of an answer
//		ACLMessage msg = receive(MessageTemplate.MatchPerformative(ACLMessage.INFORM));
//		// ONLY INFORM ??? COULD BE FUCK OFF REFUSE
//		
//		if (msg != null) {
//			//Checking if the message is from a BANK!
//			System.out.println("Recieved msg from " + msg.getSender().getLocalName());
//			if(msg.getSender().getName().equals(bankRef.getName())) {
//				System.out.println(getLocalName() + ": Woohoo I received an INFORM!");
//			}
//		}
//	}
//	
//	private boolean protocolReceiveAnswer(AID bankRef) {
//		// Waits for the arrival of an answer
//		//ACLMessage msg = receive(MessageTemplate.MatchPerformative(ACLMessage.INFORM));
//		  ACLMessage msg;
//		  msg = receive(MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), MessageTemplate.MatchProtocol(AirAgentOntology.CREATEACCOUNT)));
//		  if (msg != null)  {
//			  hasBankAccount = true;
//			  System.out.println("i have made account"); 
//			  return true;
//			  }
//		  msg = receive(MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), MessageTemplate.MatchProtocol(AirAgentOntology.DEPOSIT)));
//		  if (msg != null)  {
//			  System.out.println("i have deposited money"); 
//			  return true;
//			  }
//		  msg = receive(MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), MessageTemplate.MatchProtocol(AirAgentOntology.WITHDRAWN)));
//		  if (msg != null)  {
//			  System.out.println("i have withdrawed money"); 
//			  return true;
//			  }
//		  msg = receive(MessageTemplate.MatchPerformative(ACLMessage.INFORM));
//		  if (msg != null) {
//			  if (msg.getProtocol().equals(AirAgentOntology.CREATEACCOUNT)) {
//				  System.out.println("i am fucked and have acccount");
//				  return true;
//			  } else if (msg.getProtocol().equals(AirAgentOntology.DEPOSIT)) {
//				  System.out.println("i am fucked and have deposited money");
//				  return true;				  
//			  }
//		  }
//		  return false;
//	}	
//	
//	private void ontologicReceiveAnswer(AID bankRef) {
//		// Waits for the arrival of an answer
//		ACLMessage msg = receive(MessageTemplate.MatchPerformative(ACLMessage.INFORM));
//		// ONLY INFORM ??? COULD BE FUCK OFF REFUSE OR NOT UNDERSTOOD		
//		//Checking if the message is from a BANK!
//		if (msg != null && msg.getSender().getName().equals(bankRef.getName())) {
//			try {
//				//The ContentManager transforms the content (string) in java objects
//				ContentElement ce = null;
//				ce = getContentManager().extractContent(msg);
//				Action agAction = (Action) ce;
//				Concept conc = agAction.getAction();
//				if (conc instanceof CreateAccount) {					// trying to understand the inform
//					hasBankAccount = true;
//					System.out.println(getLocalName() + ": Woohoo I have a bank-account!");
//				} else if (conc instanceof Deposit) {
//					System.out.println(getLocalName() + ": Woohoo I deposited money");
//				} else if (conc instanceof Withdrawn) {
//					System.out.println(getLocalName() + ": Woohoo I withdrawed money");
//				}
//			} catch (CodecException cex) {
//				cex.printStackTrace();
//			} catch (OntologyException oe) {
//				oe.printStackTrace();
//			}
//		}
//	}		
//	
//	public boolean hasBankAccount(){
//		return hasBankAccount;
//	}
//}
